

Food.prototype.getPos = function (snake) {
    var x, y;
    var isValid = false;
    while (!isValid) {
        y = tools.getRandom(1, rowNum - 1);
        x = tools.getRandom(1, colNum - 1);
        isValid = true;
        var cur = snake.tail;
        while (cur) {
            if (cur.x === x && cur.y === y) {
                isValid = false;
                break;
            }
            cur = cur.next;
        }
    }
    return {
        x, y
    }
}

Food.prototype.renderFood = function (snake) {
    
    this.updateFood(snake);
    this.appendDom();
    
}

Food.prototype.updateFood = function (snake) {
    let {x,y} = this.getPos(snake);
    this.x !== undefined && (Snake.map[this.y][this.x] = 1);
    this.x = x;
    this.y = y;
    this.updateDom();
    Snake.map[y][x] = 27;
}

eventBus.on("eat",function (_this) {
    Food.prototype.updateFood.call(food,_this);
    s.addScore();
})


